package com.goddess.yeecolor.tools;

import android.app.Instrumentation;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.goddess.yeecolor.adapter.PublicFragmentViewPagerAdapter;

import static com.goddess.yeecolor.tools.MyUrl.url;

/**
 * 作者: CaoJunping
 * 时间: 2017/2/10
 * 作用: ******
 * 备注: webview cookies工具类
 */
public class CookieUtils {


    public static CookieUtils cookieUtils = null;
    public CookieManager cookieManager=null;

    public static final int SC_NOT_FOUND=403;


    public static CookieUtils  getInstence(){

        if(cookieUtils==null){

            cookieUtils=new CookieUtils();
        }
        return cookieUtils;
    }

    /**
     * 讲数据存入cookie
     * @param context 上下文
     * @return
     */
    public boolean syncCookie(Context context,String token) {
        CookieManager cookieManager = CookieManager.getInstance();
        //cookieManager.setCookie(url, "website_name="+spManager.getLong(CSJSharedPreferencesManager.KEY_USER_ID));
        //如果多cookies即添加多个setCookie();
        cookieManager.setCookie(MyUrl.JSCOOKIE,"zhangmen_token_cookie="+token);
        Log.e(MyUrl.TAG,"ffffffffff   tokenvalue    a  "+token+"  a       ffffffffffffffffffffffffffffffffffffffffffff");
        String newCookie = cookieManager.getCookie(MyUrl.JSCOOKIE);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context);
            cookieSyncManager.sync();
        }
        return TextUtils.isEmpty(newCookie) ? false : true;
    }

    /**
     * 不传Cokies设置webview相关
     * @param context
     * @param webView
     */
    public void webViewStart(Context context, WebView webView,TextView textView,String url,String token){
        webViewSet(context,webView,textView);
        this.syncCookie(context,token);
        webViewStop(context,webView,url);
    }

    /**
     * 传入Cookie设置webview相关
     * @param context
     * @param webView
     * @param textView
     */
    public void nullCookies(Context context, WebView webView,TextView textView,String url){
        webViewSet(context,webView,textView);
        webViewStop(context,webView,url);
    }

    /**
     * webview设置cookies前相关设置
     * @param context
     * @param webView
     */
    public void webViewSet(Context context, WebView webView, TextView textView){
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setAllowFileAccess(true);// 设置允许访问文件数据
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setDatabaseEnabled(true);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view,String url,String message,JsResult result){
                return super.onJsAlert(view,url,message,result);
            }
            @Override
            public void onReceivedTitle(WebView view, String title) {
                textView.setText(title);//a textview
            }

        });
    }

    /**
     * webview设置cookies后相关设置
     * @param context
     * @param webView
     */
    public void webViewStop(Context context,WebView webView,String url){
        WebSettings settings = webView.getSettings();
        String ua = settings.getUserAgentString();
        settings.setUserAgentString(ua + "; android_app/1.0.0");
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

        });
    }


    /**
     * 清除cookies
     */
    public void removeCookie(){
        if(cookieManager!=null){
            cookieManager.removeAllCookie();
        }
    }


    /**
     * 点击每一行的接口
     */
    public PublicFragmentViewPagerAdapter.OnItemClicklistener onItemClickListener;
    public interface  OnItemClicklistener{
        void OnItemClick(View view, int position);
    }

    /**
     * 单行点击事件
     * @param onItemClickListener
     */
    public void setOnItemClickListener(PublicFragmentViewPagerAdapter.OnItemClicklistener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

    /**
     * webview返回上一页
     * @param keyCode
     */
    public void actionKey(final int keyCode) {
        new Thread () {
            public void run () {
                try {
                    Instrumentation inst=new Instrumentation();
                    inst.sendKeyDownUpSync(keyCode);
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }



}
